<template>
  <div class="login">
    <div class="title">科研经费管理系统</div>
    <el-form :model="loginForm" :rules="loginRules" ref="loginForm" label-width="100px" class="demo-ruleForm">

      <el-input v-model="loginForm.username" placeholder="管理员" auto-complete="off"></el-input>
      <div style="margin: 20px 0;"></div>
      <el-input type="password" v-model="loginForm.password" placeholder="密码" auto-complete="off"></el-input>
      <div style="margin: 20px 0;"></div>
      <el-button type="primary" @click="login()" style="width:100%">登 录</el-button>

    </el-form>

  </div>
</template>

<script>
  import {login} from '@/api';
  import {mapGetters, mapMutations} from 'vuex';

  export default {
    name: 'top',
    data () {
      return {
        loginForm: {
          username: '',
          password: ''
        },
        loginRules: {
          username: [
            {required: true, message: '请输入用户名', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'}
          ]
        }
      };
    },
    computed: {
      ...mapGetters([
        'user',
        'token'
      ])
    },
    methods: {
      ...mapMutations([
        'setUser',
        'setToken'
      ]),
      login () {
        this.$refs.loginForm.validate((valid) => {
          if (valid) {
            login(this.loginForm).then(response => {
              if (response.data.status === 1) {
                this.loginFormVisible = false;
                this.setUser(response.data.data.user);
                this.setToken(response.data.data.token);
                this.$http.defaults.params = {
                  access_token: response.data.data.token
                };
                this.$router.push('/plan/list/1');
                this.$message({
                  message: '登录成功',
                  type: 'success'
                });
              } else {
                this.$message.error(response.data.message);
              }
            }).catch(error => {
              console.log(error);
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  };
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style  lang="stylus">
  body{background-color: #125f98;}
  .login span{cursor: pointer}
  .login{max-width: 300px;margin:50px auto;border:1px solid #ccc;padding:50px;background-color: #fff}
  .btn{text-align: center}
  .btn button{display: inline-block}
  .title{
    text-align: center;margin-bottom: 20px;font-size:20px;}

</style>